package com.example.om_export.system.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;
import java.util.Date;

/**
 * spring mvc解析json数据时的配置
 * json序列化和反序列化配置
 * 暂时没有使用
 * @author wb-ycl473317
 */
@JsonComponent
public class JsonConfig {

    public static class Serializer extends JsonSerializer<Date> {

        /**
         * 日期序列化成时间戳
         * @param date
         * @param jsonGenerator
         * @param serializerProvider
         * @throws IOException
         */
        @Override
        public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeNumber(date.getTime());
        }
    }

    /**
     * 字符串序列化成对象
     */
//    public static class Deserializer extends JsonDeserializer<Date> {
//
//        @Override
//        public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
//            long timestamp = jsonParser.getLongValue();
//            return new Date(timestamp);
//        }
//    }
}
